<?php
//Config : Les informations personnels de l'instance (log, pass, etc)
require("../include/config.php");

//API Fonctions : les fonctions fournis de base par l'API
require("../API/php/fonctions.php");

//Header établie la connection à la base $connection
require("../API/php/header.php");

//Fonctions : Fonctions personnelles de l'instance
require("../php/fonctions.php");

$modeDebug = false;

// phpsql/getConsoTiketCsv.php?milis=123450&ctrl=ok&dateDepart=2013-12-01&dateFin=2013-12-04

$arrayGet = array(
    "ctrl" => null,
    "dateDebut" => null,
    "dateFin" => null
);

$arrayValeur = recupInput($arrayGet, $bolDecode);

if($arrayValeur["error"] == null){
    //--------------------------------------------------------------------------
    //Pour test on récupère les paramètres de l'appli
    $strSql = "SELECT e.`code_user`, e.`incident`, IFNULL(g.`charge`,0) as 'charge'
        FROM (
            SELECT b.`code_user`, d.`incident`
            FROM (
                SELECT DISTINCT IF(a.`user` = '', 'Vide',UPPER(a.`user`)) as 'code_user'
                FROM `".$prefixTable."tab_suivihebdo_enreg` a
                WHERE 1=1
                AND a.`date_saisi` >= '".$arrayValeur["dateDebut"]."'
                AND a.`date_saisi` <= '".$arrayValeur["dateFin"]."'
                AND a.`type` = 24/*incident*/
            ) b, (
                SELECT DISTINCT IF(c.`commentaire` = '', 'Vide', SUBSTRING(c.`commentaire`,1,10)) as 'incident'
                FROM `".$prefixTable."tab_suivihebdo_enreg` c
                WHERE 1=1
                AND c.`date_saisi` >= '".$arrayValeur["dateDebut"]."'
                AND c.`date_saisi` <= '".$arrayValeur["dateFin"]."'
                AND c.`type` = 24/*incident*/
            ) d
        ) e
        LEFT OUTER JOIN (
            SELECT IF(f.`commentaire` = '', 'Vide', SUBSTRING(f.`commentaire`,1,10)) as 'incident', IF(f.`user` = '', 'Vide',UPPER(f.`user`)) as 'code_user', SUM(f.`charge`) as 'charge'
            FROM `".$prefixTable."tab_suivihebdo_enreg` f
            WHERE 1=1
            AND f.`date_saisi` >= '".$arrayValeur["dateDebut"]."'
            AND f.`date_saisi` <= '".$arrayValeur["dateFin"]."'
            AND f.`type` = 24/*incident*/
            GROUP BY f.`commentaire`, f.`user`
        ) g
        ON e.`code_user` = g.`code_user`
            AND e.`incident` = g.`incident`
        WHERE 1=1
        ORDER BY e.`code_user`, e.`incident`
    ";

    // On envois la requète
    $select = $connection->query($strSql) or die('Erreur SQL !'.$strSql.'<br>'.print_r($connection->errorInfo(), true));

    // On affiche le resultat
    $strSorti = 'code_user'.'&'.'incident'.'&'.'charge'.'&'." \r\n";
    while ($donnees = $select->fetch(PDO::FETCH_ASSOC))
    {
        $strSorti .= $donnees['code_user'].'&'.$donnees['incident'].'&'.$donnees['charge'].'&'
        ." \r\n";
    }
    $select->closeCursor();
    
    //--------------------------------------------------------------------------
    if($modeDebug){
        $strSorti .= ($strSql);
    }
}else{
    $strSorti = "ERROR:".$arrayValeur["error"];
}

require("../API/php/footer.php");
?>